package com.the_ring.chapter09.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.the_ring.chapter09.R;
import com.the_ring.chapter09.bean.ItemInfo;

import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ItemInfo> infoList;

    public GoodsAdapter(Context context, List<ItemInfo> infoList) {
        this.context = context;
        this.infoList = infoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_goods_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemHolder = (ItemViewHolder) holder;
        ItemInfo info = infoList.get(position);
        itemHolder.goodsIv.setImageResource(info.getResId());
        itemHolder.goodsTv.setText(info.getTitle());
    }

    @Override
    public int getItemCount() {
        return infoList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        public ImageView goodsIv;
        public TextView goodsTv;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsIv = itemView.findViewById(R.id.iv_goods_item);
            goodsTv = itemView.findViewById(R.id.tv_goods_item);
        }
    }
}
